package com.atguigu.gmall.product.init;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 1,使用SpringBoot的启动监听器机制
 * 2，使用组件的初始化机制
 * @ClassName InitTask
 * @Description TODO
 * @Author king
 * @Date 2022/11/9 11:41
 * @Version 1.0
 */

@Slf4j
//@Service
@AutoConfigureAfter(RedisAutoConfiguration.class)
@Configuration
public class InitTaskService {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    StringRedisTemplate redisTemplate;

//    public InitTaskService(SkuInfoService skuInfoService,
//                           StringRedisTemplate redisTemplate
//                           ){
//        log.info("InittaskService整在创建对象...");
//        this.skuInfoService = skuInfoService;
//        this.redisTemplate = redisTemplate;
//    }

    @PostConstruct
    public void initSKuIdBitMap(){
        log.info("init stask对象完全建好以后执行。。。 正准备bitmap数据");
        //查询所有skuid,
        //使用分页查询，查询所有的数据
        List<Long> ids = skuInfoService.getSkuIds();
        for (Long id : ids) {
            //所有id都去占位
            //初始胡bitmap
            redisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP_KEY, id,true);
        }

        log.info("初始化成功");
    }

}
